package com.xsy.glsurfaceview

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.opengl.GLES30
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import android.util.Log
import com.xsy.glsurfaceview.utils.ShaderUtils
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class MyGLSurfaceView2(context: Context, attributeSet: AttributeSet) :
    GLSurfaceView(context, attributeSet) {
    private var render: MyGLRender2

    init {
        setEGLContextClientVersion(3)
        val fragmentShaderCode = ShaderUtils.readFileFromAssets(context, TextureRender.fragment_file_path)
        val vertexShaderCode = ShaderUtils.readFileFromAssets(context, TextureRender.vertex_file_path)

        render = MyGLRender2(context, vertexShaderCode, fragmentShaderCode)
        setRenderer(render)
    }

    fun onDestroy() {
        render.onDestroy()
    }
}

class MyGLRender2(
    private val context: Context,
    private val vertexShaderCode: String,
    private val fragmentShaderCode: String
) :
    GLSurfaceView.Renderer {
    private lateinit var textureRender: TextureRender
    private var bitmap: Bitmap
    private var mIWidth: Int = 0
    private var mIHeight: Int = 0
    private var mSurfaceViewWidth = 0
    private var mSurfaceViewHeight = 0
    private var mViewWidth: Int = 0
    private var mViewHeight: Int = 0
    private var mStartX: Int = 0
    private var mStartY: Int = 0

    init {
        bitmap = loadImage()
        mIWidth = bitmap.width
        mIHeight = bitmap.height

    }

    private fun loadImage(): Bitmap {
        val options = BitmapFactory.Options()
        options.inScaled = false
        return BitmapFactory.decodeResource(context.resources, R.drawable.cub, options)
    }

    override fun onSurfaceCreated(gl: GL10?, config: EGLConfig?) {
        GLES30.glClearColor(1.0f, 0.0f, 0.0f, 1.0f)

        textureRender = TextureRender(bitmap, vertexShaderCode, fragmentShaderCode)
    }

    override fun onSurfaceChanged(gl: GL10?, width: Int, height: Int) {

        mSurfaceViewWidth = width
        mSurfaceViewHeight = height
        calculateViewPort()
        Log.d("MyGLSurfaceView", "width: $width")
        Log.d("MyGLSurfaceView", "height: $height")
        GLES30.glViewport(mStartX, mStartY, mViewWidth, mViewHeight)
    }

    private fun calculateViewPort() {
        val imageRatio = (mIWidth / mIHeight).toFloat()
        val surfaceRatio = (mSurfaceViewWidth / mSurfaceViewHeight).toFloat()
        //图片 接近正方形
        if (imageRatio > surfaceRatio) {
            mViewWidth = mSurfaceViewWidth
            mViewHeight = (mSurfaceViewWidth / imageRatio).toInt()
        } else if (imageRatio < surfaceRatio) {
            //图片 长方形方形
            mViewHeight = mSurfaceViewHeight
            mViewWidth = (mSurfaceViewHeight * imageRatio).toInt()
        } else {
            mViewWidth = mSurfaceViewWidth
            mViewHeight = mSurfaceViewHeight
        }
        mStartX = (mSurfaceViewWidth - mViewWidth) / 2
        mStartY = (mSurfaceViewHeight - mViewHeight) / 2
    }

    override fun onDrawFrame(gl: GL10?) {
        GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT)
        textureRender.draw()
    }

    fun onDestroy() {
        textureRender.onDestroy()
    }


}